<?php
class Pipei extends CI_Controller{

	public function __construct()
	{
		parent::__construct();
  	}

  	/**
  	 * 匹配
  	 */
   	public function index()
   	{
   		$this->load->model('order_model');
		//$this->load->model('order_model');
         //$this->load->model('user_model');
         $sysconfig = $this->order_model->pipei_sysconfig();
		
		 $pipei_num = strtotime($sysconfig['pipei_num']);
         $starttime = strtotime($sysconfig['start_time']);

         $endtime = strtotime($sysconfig['end_time']);

   		$offer_list = $this->order_model->pipei_order_list('offer');

   		$accept_list = $this->order_model->pipei_order_list('accept',$starttime,$endtime,$pipei_num);
         // var_dump($offer_list);
         // echo '<br>';
         // var_dump($accept_list);
         // exit;
   		if(!empty($accept_list) && !empty($accept_list))
   		{
   			foreach ($accept_list as $ak =>  $av)
   			{

   				foreach ($offer_list as $ok => $ov)
   				{

				  if ($av['user_id'] == $ov['user_id']) {
					  continue;
				  }
					
                  if($accept_list[$ak]['sy_money'] != 0 && $offer_list[$ok]['sy_money'] != 0){
                     
                     //$pipei_money = 0;
					 
					// var_dump($av);//exit();
					 
                     echo $av['id'].'获得-->:'.$accept_list[$ak]['sy_money'];
                     echo '<br>';
                     echo $ov['id'].'提供-->:'.$offer_list[$ok]['sy_money'];
                     echo '<br>';
                     if($accept_list[$ak]['sy_money'] > $offer_list[$ok]['sy_money'])
                     {
                        $pipei_money = $offer_list[$ok]['sy_money'];
                     }
                     else
                     {
                        $pipei_money = $accept_list[$ak]['sy_money'];
                     }
                     echo '匹配:'.$pipei_money;
                     echo '<br>';
                     $accept_list[$ak]['sy_money'] -= $pipei_money;

                     $offer_list[$ok]['sy_money'] -= $pipei_money;

                     $bianhao = $this->common->getnumChar('7');
					 
                     
					 
                     $day_num = floor( (time() - $ov['create_time']) / 86400);
                     $day_num = $day_num < 6 ? $day_num : 6;
                     $result = $this->order_model->order_log_insert($ov['id'],$av['id'],$pipei_money,$bianhao,$day_num);

                     $this->order_model->offer_order_symoney_update(($offer_list[$ok]['sy_money']),$ov['id']);

                     $this->order_model->accept_order_symoney_update(($accept_list[$ak]['sy_money']),$av['id']);

                     if($result)
                     {
                        echo '成功<br>';
                     }
                     else
                     {
                        echo '失败<br>';
                     }
                     echo $av['id'].'获得剩余:'.$accept_list[$ak]['sy_money'];
                     echo '<br>';
                     echo $ov['id'].'提供剩余:'.$offer_list[$ok]['sy_money'];
                     echo '------------------------------------<br><br>';
                     //send_sms
                     // 收款方匹配
                     $users = $this->user->get_user_info_id($av['user_id']);
                     $mobile = $users['mobile'];
                     $content = '您的“接受公益帮助”订单已匹配成功，3小时后可查看匹配人信息，请登录平台进行相关操作。';
                     $this->common->send_sms($mobile,$content);
                     // 打款方匹配
                     $users = $this->user->get_user_info_id($ov['user_id']);
                     $mobile = $users['mobile'];
                     $content = '您的“提供公益帮助”订单已匹配成功，5小时内完成合作可获得相应积分奖励，请登录平台进行相关操作。';
                     $this->common->send_sms($mobile,$content);
                  }
                  
   				}
   			}
   		}
   	}
}
